package be.xios.projectquiz.jury;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import be.xios.projectquiz.engine.EngineDB;

/// <summary>
/// Deze klasse verkrijgt de top 10 van de zaalquiz spellen.
/// </summary>
/// <author>Flemming Bosch</author>
public class Top10Blokken extends JFrame{
	private static final long serialVersionUID = 1L;
	//DB info
	//private static String DRIVER ="com.mysql.jdbc.Driver";
	private static String HOST ="jdbc:mysql://localhost:3306/projectdb";
	//private static String HOST = "jdbc:mysql://91.177.82.110:3306/projectdb";
	private static String USER ="Master";
	private static String PASSWORD ="xios";
	private static final String QUERY = "SELECT p.`persoon_voornaam` AS Voornaam,p.`persoon_naam` AS Naam,s.`score` as score FROM `spelerscore` s,`personen`p WHERE s.`persoon_id`=p.`persoon_id` ORDER BY score DESC LIMIT 10";
	private EngineDB edb;	
	
	//GUI Componenten
	private JPanel mid;
	private JTable table;
	private JButton generate;
	private JButton close;	
	private JLabel status;
	private ResultSetTableModel tableModel=null;
	
	/// <summary>
	/// Dit is de default constructor waar de GUI word in aangemaakt.
	/// </summary>
	/// <author>Flemming Bosch</author>
	public Top10Blokken() {
		super("Top 10 - Blokken");
		setLayout(new BorderLayout());
			
		//MIDPANEL
		mid = new JPanel();
		//JLABEL status
		status = new JLabel();
			
		try{
			tableModel = new ResultSetTableModel(HOST, USER, PASSWORD, QUERY);
			table = new JTable(tableModel);
		}
		catch(SQLException ex){
			JOptionPane.showMessageDialog(null, ex.getMessage(),"Database Error",JOptionPane.ERROR_MESSAGE);
			if(tableModel!=null){
				tableModel.disconnectFromDatabase();
				this.dispose();
			}	
		}
			
		generate = new JButton("Genereer XML-bestand");
		generate.addActionListener(new ActionListener()
		{
			public void actionPerformed( ActionEvent event ) {
				if(event.getSource().equals(generate)){
					edb = new EngineDB();
					try {
						edb.genereerXML("topscores","BLOKKEN");
						status.setText("Top 10 Blokken is succesvol gegenereerd!");
					}
					catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		});
		close = new JButton("Sluit venster");
		close.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				if(e.getSource().equals(close)){
					dispose();
				}
			}
		});
			
		//JTable en de buttons toevoegen aan de MID-panel
		mid.add(new JScrollPane(table));
		mid.add(generate);
		mid.add(close);
		
		//Toevoegen aan Frame
		add(mid,BorderLayout.CENTER);
		add(status,BorderLayout.SOUTH);
	}
}